package com.java.admin.serviceType.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.admin.serviceType.entity.AddEntity;
import com.java.database.JunUtil;
import com.java.database.entity.Servicetype;
import com.java.database.mapper.ServicetypeMapper;
import com.java.database.mapper.ServicetypeSpareMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class ServeService {
    @Autowired
    private ServicetypeMapper servicetypeMapper;

    @Autowired
    private ServicetypeSpareMapper servicetypeSpareMapper;
    public int add(Servicetype servicetype) {
        servicetype.setServicetypeid(0);
        servicetype.setStatus(0);
        int insert =0;
        try {
            insert = servicetypeMapper.insert(servicetype);
        }catch (Exception e){
            throw new RuntimeException("添加服务类型异常");
        }
       return insert;
    }

    public PageInfo queryAll(String pageNum,String servicetypename) {
        if(JunUtil.ifNull(pageNum)){
            pageNum = "1";
        };
        int pagenum= Integer.parseInt(pageNum);
        PageHelper.startPage(pagenum, 5);
        List<Servicetype> servicetypes = servicetypeMapper.selectAll(servicetypename);
        PageInfo page = new PageInfo(servicetypes);
        if(page==null){
            throw new RuntimeException("查询失败");
        }
        return page;
    }

    public int remove(String servicetypeid) {
        int serviceTypeId = Integer.parseInt(servicetypeid);
        int remove =0;
        try {
            remove = servicetypeMapper.removeByPrimaryKey(serviceTypeId);
        }catch (Exception e){
            throw new RuntimeException("添加服务类型异常");
        }
        return remove;
    }

    public int update(Servicetype servicetype) {
        servicetype.setStatus(0);
        int update = 0;
        try {
            update = servicetypeMapper.updateByPrimaryKey(servicetype);
        }catch (Exception e){
            throw new RuntimeException("修改服务类型失败");
        }
       return update;
    }

    public Servicetype queryServeById(String servicetypeid) {
        if(servicetypeid==null||"".equals(servicetypeid)){
            servicetypeid="1";
        }
        int servicetypeId = Integer.parseInt(servicetypeid);
        Servicetype servicetype = servicetypeMapper.selectByPrimaryKey(servicetypeId);
        return servicetype;
    }

    /**
     *  将添加实体的信息存入数据库
     * @param addEntity
     */
    public void addServiceType(AddEntity addEntity){
       Integer count = servicetypeMapper.addserviceType(addEntity);
       if(count <=0){
           throw new RuntimeException("类型添加失败");
       }
       Servicetype servicetype = servicetypeMapper.queryServiceTypeByNewAdd(addEntity);
       if(servicetype == null){
           throw new RuntimeException("刚刚添加的类型没有找到哦");
       }
        Map myMap = new HashMap();
        myMap.put("servicetypeId",servicetype.getServicetypeid());
        myMap.put("spareIds",addEntity.getResultSpareIds());
        myMap.put("counts",addEntity.getResultCounts());
//        for(int i =0;i<addEntity.getResultSpareIds().size();i++){
//            Integer  spareId = addEntity.getResultSpareIds().get(i);
//            Integer count2 = addEntity.getResultCounts().get(i);
//            myMap.put(spareId, count2);
//        }
       Integer count1 = servicetypeSpareMapper.addByServiceType(myMap);
    }
}
